package com.tind.wyf.baselib.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * <pre>
 *     @author : tind
 *     @e-mail : xxx@xx
 *     @time   : 2017/11/24
 *     @desc   : BaseFragment
 *              {@link #}
 *     @version: 1.0
 * </pre>
 */

public abstract class BaseFragment extends Fragment {
    protected final String TAG = this.getClass().getSimpleName();
    private View view;
    protected Context mContext;
    public Activity activity;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBundle(getArguments());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(getLayoutId(), container, false);
            initView(view);
            initData();
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (null != parent) {
                parent.removeView(view);
            }
        }
        return view;
    }

    public <T extends View> T $(@IdRes int viewId) {
        if (view != null) {
            return view.findViewById(viewId);
        }
        throw new NullPointerException("rootView is null");
    }

    @LayoutRes
    protected abstract int getLayoutId();

    protected abstract void initView(View view);

    protected abstract void initData();

    protected void initBundle(Bundle arguments) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
